﻿var express = require('express');
var pg = require('pg');
var app = express();
var fs = require('fs');

var queryString = require('querystring');

var conString = "tcp://postgres:postgres@19.16.71.127/db2";
var conString2 = "tcp://postgres:postgres@19.16.71.127/db3";



app.all('*', function(req, res, next) {
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "X-Requested-With");
    res.header("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS");
    res.header("X-Powered-By",' 3.2.1')
    res.header("Content-Type", "application/json;charset=utf-8");
    next();
});


app.get('/videoPlay',function(req,res){
   var time = new Date();
   var videoName=req.query.name;
  console.log("-------视频播放请求"+ time.getFullYear() + "/" + time.getMonth() + "/" + time.getDate() + "/" + time.getHours() + "/" + time.getMinutes() + "/" + time.getSeconds() + "-------");
  res.writeHead(200, {'Content-Type': 'video/mp4'});
   var rs = fs.createReadStream(videoName+'.mp4');
   rs.pipe(res);
          rs.on('end',function(){
            res.end();
            console.log('end call');
          });
})



//点击查询服务-延误报警器
app.get('/getSmokeBySingleClick', function(req, res) {
  var time = new Date();
  var geoText = req.query.geoText;
  console.log("-------点击查询下载"+geoText+ time.getFullYear() + "/" + time.getMonth() + "/" + time.getDate() + "/" + time.getHours() + "/" + time.getMinutes() + "/" + time.getSeconds() + "-------");

  var message = getSmokeBySingleClick(function(json) {


    res.send(json);
  },geoText)
})

//点击查询服务-延误报警器
app.get('/getPowerBySingleClick', function(req, res) {
  var time = new Date();
  var geoText = req.query.geoText;
  console.log("-------点击查询下载"+geoText+ time.getFullYear() + "/" + time.getMonth() + "/" + time.getDate() + "/" + time.getHours() + "/" + time.getMinutes() + "/" + time.getSeconds() + "-------");

  var message = getPowerBySingleClick(function(json) {

    res.send(json);
  },geoText)
})
//点击查询服务-video
app.get('/getVideoBySingleClick', function(req, res) {
  var time = new Date();
  var geoText = req.query.geoText;
  console.log("-------video点击查询"+geoText+ time.getFullYear() + "/" + time.getMonth() + "/" + time.getDate() + "/" + time.getHours() + "/" + time.getMinutes() + "/" + time.getSeconds() + "-------");

  var message = getVideoBySingleClick(function(json) {

    res.send(json);
  },geoText)
})
//点击查询服务-重点单位
app.get('/ImportantEnterprise', function(req, res) {
  var time = new Date();
  var geoText = req.query.geoText;
  console.log("------重点单位下载"+geoText+ time.getFullYear() + "/" + time.getMonth() + "/" + time.getDate() + "/" + time.getHours() + "/" + time.getMinutes() + "/" + time.getSeconds() + "-------");

  var message = getImEnterpriseBySingleClick(function(json) {
    res.send(json);
  },geoText)
})

app.get('/getAllImEnterprise', function(req, res) {
var time = new Date();
  console.log("-------重点单位查询" + time.getFullYear() + "/" + time.getMonth() + "/" + time.getDate() + "/" + time.getHours() + "/" + time.getMinutes() + "/" + time.getSeconds() + "-------");
  var message = getAllImEnterprise(function(json) {
    res.send(json);
  })
})

app.get('/getAllTrain', function(req, res) {
var time = new Date();
  console.log("-------消防培训查询" + time.getFullYear() + "/" + time.getMonth() + "/" + time.getDate() + "/" + time.getHours() + "/" + time.getMinutes() + "/" + time.getSeconds() + "-------");
  var message = getAllTrain(function(json) {
    res.send(json);
  })
})

app.get('/DownLoadAPK', function(req, res) {
  //var name = req.query.username;
  //var pass = req.query.userpass;
  var time = new Date();
  console.log("-------文件下载" + time.getFullYear() + "/" + time.getMonth() + "/" + time.getDate() + "/" + time.getHours() + "/" + time.getMinutes() + "/" + time.getSeconds() + "-------");
  res.download('app-release.apk');

})

app.get('/Version', function(req, res) {
  //var name = req.query.username;
  //var pass = req.query.userpass;
  var time = new Date();
  console.log("-------版本检测下载" + time.getFullYear() + "/" + time.getMonth() + "/" + time.getDate() + "/" + time.getHours() + "/" + time.getMinutes() + "/" + time.getSeconds() + "-------");

  var json = [{
    name:"4",
    version:4,
    update:"功能更新",
    date:"2018年1月10日"
  }];
  res.send(json);
})

app.get('/getFacilities', function(req, res) {
var time = new Date();
  console.log("-------报警器设备查询" + time.getFullYear() + "/" + time.getMonth() + "/" + time.getDate() + "/" + time.getHours() + "/" + time.getMinutes() + "/" + time.getSeconds() + "-------");
  var message = getFacilities(function(json) {
    res.send(json);
  })
})

app.get('/getInspection', function(req, res) {
var time = new Date();
  console.log("-------巡检记录查询" + time.getFullYear() + "/" + time.getMonth() + "/" + time.getDate() + "/" + time.getHours() + "/" + time.getMinutes() + "/" + time.getSeconds() + "-------");
  var message = getInspection(function(json) {
    res.send(json);
  })
})

app.get('/getSafePerson', function(req, res) {
var time = new Date();
  console.log("-------巡检记录查询" + time.getFullYear() + "/" + time.getMonth() + "/" + time.getDate() + "/" + time.getHours() + "/" + time.getMinutes() + "/" + time.getSeconds() + "-------");
  var message = getSafePerson(function(json) {
    res.send(json);
  })
})

app.get('/alarm', function(req, res) {
var time = new Date();
  console.log("-------报警器设备查询" + time.getFullYear() + "/" + time.getMonth() + "/" + time.getDate() + "/" + time.getHours() + "/" + time.getMinutes() + "/" + time.getSeconds() + "-------");
  var message = getAllAlarm(function(json) {
    res.send(json);
  })
})

app.get('/video', function(req, res) {
  var time = new Date();
  console.log("-------视频设备查询" + time.getFullYear() + "/" + time.getMonth() + "/" + time.getDate() + "/" + time.getHours() + "/" + time.getMinutes() + "/" + time.getSeconds() + "-------");
  var message = getAllVideo(function(json) {
    res.send(json);
  })
})

app.get('/door', function(req, res) {
  var time = new Date();
  console.log("-------防火门设备查询" + time.getFullYear() + "/" + time.getMonth() + "/" + time.getDate() + "/" + time.getHours() + "/" + time.getMinutes() + "/" + time.getSeconds() + "-------");
  var message = getAllDoor(function(json) {
    res.send(json);
  })
})

app.get('/temp', function(req, res) {
  var time = new Date();
  console.log("-------温度设备查询" + time.getFullYear() + "/" + time.getMonth() + "/" + time.getDate() + "/" + time.getHours() + "/" + time.getMinutes() + "/" + time.getSeconds() + "-------");
  var message = getAllTemp(function(json) {
    res.send(json);
  })
})

app.get('/adivision', function(req, res) {
  var time = new Date();
  console.log("-------行政区查询" + time.getFullYear() + "/" + time.getMonth() + "/" + time.getDate() + "/" + time.getHours() + "/" + time.getMinutes() + "/" + time.getSeconds() + "-------");
  var message = getAllADivision(function(json) {
    res.send(json);
  })
})

app.get('/otherdivision', function(req, res) {
  var time = new Date();
  console.log("-------其他划分查询" + time.getFullYear() + "/" + time.getMonth() + "/" + time.getDate() + "/" + time.getHours() + "/" + time.getMinutes() + "/" + time.getSeconds() + "-------");
  var message = getOtherDivision(function(json) {
    res.send(json);
  })
})

app.get('/waterlevel', function(req, res) {
  var time = new Date();
  console.log("-------水位查询" + time.getFullYear() + "/" + time.getMonth() + "/" + time.getDate() + "/" + time.getHours() + "/" + time.getMinutes() + "/" + time.getSeconds() + "-------");
  var message = getAllWaterLevel(function(json) {
    res.send(json);
  })
})

app.get('/enterprise', function(req, res) {
  var time = new Date();
  console.log("-------企业分布查询" + time.getFullYear() + "/" + time.getMonth() + "/" + time.getDate() + "/" + time.getHours() + "/" + time.getMinutes() + "/" + time.getSeconds() + "-------");
  var message = getAllEnterprise(function(json) {
    res.send(json);
  })
})

app.get('/enterpriseByName', function(req, res) {
  var time = new Date();
  var name = req.query.name;
  console.log("-------根据企业名称查询" + time.getFullYear() + "/" + time.getMonth() + "/" + time.getDate() + "/" + time.getHours() + "/" + time.getMinutes() + "/" + time.getSeconds() + "-------");
  var message = getEnterpriseByName(function(json) {
    res.send(json);
  }, name)
})

app.get('/power', function(req, res) {
  var time = new Date();
  console.log("-------电力分布查询" + time.getFullYear() + "/" + time.getMonth() + "/" + time.getDate() + "/" + time.getHours() + "/" + time.getMinutes() + "/" + time.getSeconds() + "-------");
  var message = getAllPower(function(json) {
    res.send(json);
  })
})

app.get('/getAlarmHistory', function(req, res) {
  var time = new Date();
  console.log("-------报警查询" + time.getFullYear() + "/" + time.getMonth() + "/" + time.getDate() + "/" + time.getHours() + "/" + time.getMinutes() + "/" + time.getSeconds() + "-------");
  var message = getAlarmHistory(function(json) {
    res.send(json);
  })
})

app.get('/smoke', function(req, res) {
  var time = new Date();
  console.log("-------烟雾分布查询" + time.getFullYear() + "/" + time.getMonth() + "/" + time.getDate() + "/" + time.getHours() + "/" + time.getMinutes() + "/" + time.getSeconds() + "-------");
  var message = getAllSmoke(function(json) {
    res.send(json);
  })
})

app.get('/spot', function(req, res) {
  var time = new Date();
  console.log("-------抽查记录查询" + time.getFullYear() + "/" + time.getMonth() + "/" + time.getDate() + "/" + time.getHours() + "/" + time.getMinutes() + "/" + time.getSeconds() + "-------");
  var message = getSpotList(function(json) {
    res.send(json);
  })
})


app.get('/district', function(req, res) {
  var time = new Date();
  console.log("-------行政区分布查询" + time.getFullYear() + "/" + time.getMonth() + "/" + time.getDate() + "/" + time.getHours() + "/" + time.getMinutes() + "/" + time.getSeconds() + "-------");
  var message = getAllDistrict(function(json) {
    res.send(json);
  })
})

app.get('/latlng', function(req, res) {
  var time = new Date();
  console.log("-------经纬网坐标查询" + time.getFullYear() + "/" + time.getMonth() + "/" + time.getDate() + "/" + time.getHours() + "/" + time.getMinutes() + "/" + time.getSeconds() + "-------");
  var message = getAllLatLng(function(json) {
    res.send(json);
  })
})

app.get('/invoice', function(req, res) {
  var time = new Date();
  console.log("-------发票查询" + time.getFullYear() + "/" + time.getMonth() + "/" + time.getDate() + "/" + time.getHours() + "/" + time.getMinutes() + "/" + time.getSeconds() + "-------");
  var message = getAllInvoice(function(json) {
    res.send(json);
  })
})
app.get('/enterpriseAccess', function(req, res) {
  var time = new Date();
  console.log("-------申请企业查询" + time.getFullYear() + "/" + time.getMonth() + "/" + time.getDate() + "/" + time.getHours() + "/" + time.getMinutes() + "/" + time.getSeconds() + "-------");
  var message = getEnterpriseAccess(function(json) {
    res.send(json);
  })
})

app.get('/getCheckList', function(req, res) {
  var time = new Date();
  console.log("-------申请企业查询" + time.getFullYear() + "/" + time.getMonth() + "/" + time.getDate() + "/" + time.getHours() + "/" + time.getMinutes() + "/" + time.getSeconds() + "-------");
  var message = getCheckList(function(json) {
    res.send(json);
  })
})

app.get('/getTrain', function(req, res) {
  var time = new Date();
  console.log("-------培训记录查询" + time.getFullYear() + "/" + time.getMonth() + "/" + time.getDate() + "/" + time.getHours() + "/" + time.getMinutes() + "/" + time.getSeconds() + "-------");
  var message = getTrain(function(json) {
    res.send(json);
  })
})
app.get('/getRepair', function(req, res) {
  var time = new Date();
  console.log("-------维修记录查询" + time.getFullYear() + "/" + time.getMonth() + "/" + time.getDate() + "/" + time.getHours() + "/" + time.getMinutes() + "/" + time.getSeconds() + "-------");
  var message = getRepair(function(json) {
    res.send(json);
  })
})
app.get('/getBreakDown', function(req, res) {
  var time = new Date();
  console.log("-------设备故障查询" + time.getFullYear() + "/" + time.getMonth() + "/" + time.getDate() + "/" + time.getHours() + "/" + time.getMinutes() + "/" + time.getSeconds() + "-------");
  var message = getBreakDown(function(json) {
    res.send(json);
  })
})
app.get('/getFPSRecord', function(req, res) {
  var time = new Date();
  console.log("-------巡检记录查询" + time.getFullYear() + "/" + time.getMonth() + "/" + time.getDate() + "/" + time.getHours() + "/" + time.getMinutes() + "/" + time.getSeconds() + "-------");
  var message = getFPSRecord(function(json) {
    res.send(json);
  })
})

app.get('/getInspectionMission', function(req, res) {
  var time = new Date();
  console.log("-------巡检任务查询" + time.getFullYear() + "/" + time.getMonth() + "/" + time.getDate() + "/" + time.getHours() + "/" + time.getMinutes() + "/" + time.getSeconds() + "-------");
  var message = getInspectionMission(function(json) {
    res.send(json);
  })
})


var server = app.listen(8081, function() {

  var host = server.address().address;
  var port = server.address().port;

  console.log("应用实例，访问地址为 http://%s:%s", host, port);

})

var io = require('socket.io').listen(server);

io.sockets.on('connection', function (socket) {
  console.log("服务已连接");
    socket.on('name', function (data) {
        console.log(data.room);
        socket.join(data.room);
    });

    socket.on('disconnect', function () {
console.log("服务已断开");
    });

    socket.on('message:send', function (data) {
console.log("发送信息");
        var from = data.from;
        console.log(data.message+"  "+from);
        socket.broadcast.to(from).emit('message', { 'message':data.message });

    });

});


function test(callback) {
  var client = new pg.Client(conString);
  client.connect(function(err) {
    console.error('连接出错', err);
    client.query("select * from fire_Purchase", function(err, result) {
      console.error('查询出错', err);
      console.log(result);
      callback(result);
    })
  });
  client.end();
}
/**
 * [获取所有行政区数据]
 * @param  {Function} callback [description]
 * @return {[type]}            [description]
 */
function getAllADivision(callback) {
  var client = new pg.Client(conString);
  client.connect(function(err) {
    if (err) {
      console.error('连接出错', err);
    }

    client.query("select company, address, code, ST_AsGeoJSON(geom) as geom from video", function(err, result) {
      if (err) {
        console.error('查询出错', err);
      }
      callback(result.rows);
      client.end();
    })
  });
}

/**
 * [获取所有视频数据]
 * @param  {Function} callback [description]
 * @return {[type]}            [description]
 */
function getAllVideo(callback) {
  var client = new pg.Client(conString);
  client.connect(function(err) {
    if (err) {
      console.error('连接出错', err);
    }

    client.query("select company, address, code, ST_AsGeoJSON(geom) as geom from video", function(err, result) {
      if (err) {
        console.error('查询出错', err);
      }
      callback(result.rows);
      client.end();
    })
  });
}
/**
 * [获取所有温度数据]
 * @param  {Function} callback [description]
 * @return {[type]}            [description]
 */
function getAllTemp(callback) {
  var client = new pg.Client(conString);
  client.connect(function(err) {
    if (err) {
      console.error('连接出错', err);
    }

    client.query("select company, address, code, temp, ST_AsGeoJSON(geom) as geom from thermometer", function(err, result) {
      if (err) {
        console.error('查询出错', err);
      }
      callback(result.rows);
      client.end();
    })
  });
}

/**
 * [获取所有防火门数据]
 * @param  {Function} callback [description]
 * @return {[type]}            [description]
 */
function getAllDoor(callback) {
  var client = new pg.Client(conString);
  client.connect(function(err) {
    if (err) {
      console.error('连接出错', err);
    }

    client.query("select company, address, code, state, ST_AsGeoJSON(geom) as geom from firedoor", function(err, result) {
      if (err) {
        console.error('查询出错', err);
      }
      callback(result.rows);
      client.end();
    })
  });
}

/**
 * [获取所有报警数据]
 * @param  {Function} callback [description]
 * @return {[type]}            [description]
 */
function getAllAlarm(callback) {
  var client = new pg.Client(conString);
  client.connect(function(err) {
    if (err) {
      console.error('连接出错', err);
    }

    client.query("select company, address, code, state, ST_AsGeoJSON(geom) as geom from alerm", function(err, result) {
      if (err) {
        console.error('查询出错', err);
      }
      callback(result.rows);
      client.end();
    })
  });

}

/**
 * [获取所有水位监控数据]
 * @param  {Function} callback [description]
 * @return {[type]}            [description]
 */
function getAllWaterLevel(callback) {
  var client = new pg.Client(conString);
  client.connect(function(err) {
    if (err) {
      console.error('连接出错', err);
    }

    client.query("select company, address, code, state, ST_AsGeoJSON(geom) as geom from firewaterlevel", function(err, result) {
      if (err) {
        console.error('查询出错', err);
      }
      callback(result.rows);
      client.end();
    })
  });
}

/**
 * [获取所有其他划分区域数据]
 * @param  {Function} callback [description]
 * @return {[type]}            [description]
 */
function getOtherDivision(callback) {
  var client = new pg.Client(conString);
  client.connect(function(err) {
    if (err) {
      console.error('连接出错', err);
    }

    client.query("select gid, ST_AsGeoJSON(geom) as geom from mgridpoint", function(err, result) {
      if (err) {
        console.error('查询出错', err);
      }
      callback(result.rows);
      client.end();
    })
  });
}

/**
 * [获取所有企业分布数据]
 * @param  {Function} callback [description]
 * @return {[type]}            [description]
 */
function getAllEnterprise(callback) {
  var client = new pg.Client(conString);
  client.connect(function(err) {
    if (err) {
      console.error('连接出错', err);
    }
    client.query("select name, admregion, ST_AsGeoJSON(geom) as geom from fire_enterprise", function(err, result) {
      if (err) {
        console.error('查询出错', err);
      }
      callback(result.rows);
      client.end();
    })
  });

}

/**
 * [获取指定企业数据]
 * @param  {Function} callback [description]
 * @return {[type]}            [description]
 */
function getEnterpriseByName(callback, name) {
  var client = new pg.Client(conString);
  client.connect(function(err) {
    if (err) {
      console.error('连接出错', err);
    }
    client.query("select * from fire_enterprise where name='" + name + "'", function(err, result) {
      if (err) {
        console.error('查询出错', err);
      }
      callback(result.rows);
      client.end();
    })
  });

}

/**
 * [获取所有烟雾数据]
 * @param  {Function} callback [description]
 * @return {[type]}            [description]
 */
function getAllSmoke(callback) {
  var client = new pg.Client(conString);
  client.connect(function(err) {
    if (err) {
      console.error('连接出错', err);
    }
    client.query("select company, address, smoke, ST_AsGeoJSON(geom) as geom from smoke", function(err, result) {
      if (err) {
        console.error('查询出错', err);
      }
      callback(result.rows);
      client.end();
    })
  });
}

/**
 * [获取所有电压数据]
 * @param  {Function} callback [description]
 * @return {[type]}            [description]
 */
function getAllPower(callback) {
  var client = new pg.Client(conString);
  client.connect(function(err) {
    if (err) {
      console.error('连接出错', err);
    }
    client.query("select company, address, smoke, ST_AsGeoJSON(geom) as geom from power", function(err, result) {
      if (err) {
        console.error('查询出错', err);
      }
      callback(result.rows);
      client.end();
    })
  });

}

/**
 * [获取所有行政区数据]
 * @param  {Function} callback [description]
 * @return {[type]}            [description]
 */
function getAllDistrict(callback) {
  var client = new pg.Client(conString);
  client.connect(function(err) {
    if (err) {
      console.error('连接出错', err);
    }
    client.query("select name, adname, ST_AsGeoJSON(geom) as geom from district", function(err, result) {
      if (err) {
        console.error('查询出错', err);
      }
      callback(result.rows);
      client.end();
    })
  });
}

/**
 * [获取经纬网位置数据]
 * @param  {Function} callback [description]
 * @return {[type]}            [description]
 */
function getAllLatLng(callback) {
  var client = new pg.Client(conString);
  client.connect(function(err) {
    if (err) {
      console.error('连接出错', err);
    }
    client.query("select coordinate, ST_AsGeoJSON(geom) as geom from gridpoint", function(err, result) {
      if (err) {
        console.error('查询出错', err);
      }
      callback(result.rows);
      client.end();
    })
  });
}

/**
 * [获取发票数据]
 * @param  {Function} callback [description]
 * @return {[type]}            [description]
 */
function getAllInvoice(callback) {
  var client = new pg.Client(conString);
  client.connect(function(err) {
    if (err) {
      console.error('连接出错', err);
    }
    client.query("select * from fire_invoice", function(err, result) {
      if (err) {
        console.error('查询出错', err);
      }
      callback(result.rows);
      client.end();
    })
  });
}

/**
 * [获取申请企业数据]
 * @param  {Function} callback [description]
 * @return {[type]}            [description]
 */
function getEnterpriseAccess(callback) {
  var client = new pg.Client(conString);
  client.connect(function(err) {
    if (err) {
      console.error('连接出错', err);
    }
    client.query("select * from fire_registeredenterprise", function(err, result) {
      if (err) {
        console.error('查询出错', err);
      }
      callback(result.rows);
      client.end();
    })
  });
}


/**
 * [获取审核记录数据]
 * @param  {Function} callback [description]
 * @return {[type]}            [description]
 */
function getCheckList(callback) {
  var client = new pg.Client(conString);
  client.connect(function(err) {
    if (err) {
      console.error('连接出错', err);
    }
    client.query("select * from fire_enterpriseaudit", function(err, result) {
      if (err) {
        console.error('查询出错', err);
      }
      callback(result.rows);
      client.end();
    })
  });
}



/**
 * [获取抽查记录数据]
 * @param  {Function} callback [description]
 * @return {[type]}            [description]
 */
function getSpotList(callback) {
  var client = new pg.Client(conString);
  client.connect(function(err) {
    if (err) {
      console.error('连接出错', err);
    }
    client.query("select * from fire_spotproblem", function(err, result) {
      if (err) {
        console.error('查询出错', err);
      }
      callback(result.rows);
      client.end();
    })
  });
}

/**
 * [获取抽查记录数据]
 * @param  {Function} callback [description]
 * @return {[type]}            [description]
 */
function getSpotList(callback) {
  var client = new pg.Client(conString);
  client.connect(function(err) {
    if (err) {
      console.error('连接出错', err);
    }
    client.query("select * from fire_spotproblem", function(err, result) {
      if (err) {
        console.error('查询出错', err);
      }
      callback(result.rows);
      client.end();
    })
  });
}

/**
 * [获取抽查记录数据]
 * @param  {Function} callback [description]
 * @return {[type]}            [description]
 */
function getTrain(callback) {
  var client = new pg.Client(conString);
  client.connect(function(err) {
    if (err) {
      console.error('连接出错', err);
    }
    client.query("select * from fire_train", function(err, result) {
      if (err) {
        console.error('查询出错', err);
      }
      callback(result.rows);
      client.end();
    })
  });
}

/**
 * [获取抽查记录数据]
 * @param  {Function} callback [description]
 * @return {[type]}            [description]
 */
function getRepair(callback) {
  var client = new pg.Client(conString);
  client.connect(function(err) {
    if (err) {
      console.error('连接出错', err);
    }
    client.query("select * from fire_repairrecord", function(err, result) {
      if (err) {
        console.error('查询出错', err);
      }
      callback(result.rows);
      client.end();
    })
  });
}

/**
 * [获取抽查记录数据]
 * @param  {Function} callback [description]
 * @return {[type]}            [description]
 */
function getBreakDown(callback) {
  var client = new pg.Client(conString);
  client.connect(function(err) {
    if (err) {
      console.error('连接出错', err);
    }
    client.query("select * from fire_breakdown", function(err, result) {
      if (err) {
        console.error('查询出错', err);
      }
      callback(result.rows);
      client.end();
    })
  });
}

/**
 * [获取抽查记录数据]
 * @param  {Function} callback [description]
 * @return {[type]}            [description]
 */
function getFPSRecord(callback) {
  var client = new pg.Client(conString);
  client.connect(function(err) {
    if (err) {
      console.error('连接出错', err);
    }
    client.query("select * from fire_fpstourrecord", function(err, result) {
      if (err) {
        console.error('查询出错', err);
      }
      callback(result.rows);
      client.end();
    })
  });
}

/**
 * [获取消防设备数据]
 * @param  {Function} callback [description]
 * @return {[type]}            [description]
 */
function getFacilities(callback) {
  var client = new pg.Client(conString);
  client.connect(function(err) {
    if (err) {
      console.error('连接出错', err);
    }
    client.query("select * from fire_facilities", function(err, result) {
      if (err) {
        console.error('查询出错', err);
      }
      callback(result.rows);
      client.end();
    })
  });
}

/**
 * [获取消防设备数据]
 * @param  {Function} callback [description]
 * @return {[type]}            [description]
 */
function getInspection(callback) {
  var client = new pg.Client(conString);
  client.connect(function(err) {
    if (err) {
      console.error('连接出错', err);
    }
    client.query("select worker, date, back, ST_AsGeoJSON(geom) from inspection", function(err, result) {
      if (err) {
        console.error('查询出错', err);
      }
      callback(result.rows);
      client.end();
    })
  });
}
/**
 * [点击查询烟雾报警器]
 * @param  {Function} callback [description]
 * @return {[type]}            [description]
 */
function getSmokeBySingleClick(callback,geoText){
  var client = new pg.Client(conString);
  client.connect(function(err) {
    if (err) {
      console.error('连接出错', err);
    }
    client.query("select smoke, ST_AsGeoJSON(geom) as geom, company, address from smoke where ST_DWithin(geom,ST_Geometryfromtext('"+geoText+"',4326),15) order by ST_Distance(geom,ST_GeometryFromText('"+geoText+"',4326))  limit 1", function(err, result) {
      if (err) {
        console.error('查询出错', err);
      }
      callback(result.rows);
      client.end();
    })
  });
}

/**
 * [点击查询电力报警器]
 * @param  {Function} callback [description]
 * @return {[type]}            [description]
 */
function getPowerBySingleClick(callback,geoText){
  var client = new pg.Client(conString);
  client.connect(function(err) {
    if (err) {
      console.error('连接出错', err);
    }
    client.query("select smoke, ST_AsGeoJSON(geom) as geom, company, address from power t order by ST_Distance( t.geom, ST_GeometryFromText('"+geoText+"',4326))  limit 1", function(err, result) {
      if (err) {
        console.error('查询出错', err);
      }
      callback(result.rows);
      client.end();
    })
  });
}

/**
 * [点击查询video]
 * @param  {Function} callback [description]
 * @return {[type]}            [description]
 */
function getVideoBySingleClick(callback,geoText){
  var client = new pg.Client(conString);
  client.connect(function(err) {
    if (err) {
      console.error('连接出错', err);
    }
    client.query("select code, ST_AsGeoJSON(geom) as geom, company, address from video t order by ST_Distance( t.geom, ST_GeometryFromText('"+geoText+"',4326))  limit 1", function(err, result) {
      if (err) {
        console.error('查询出错', err);
      }
      callback(result.rows);
      client.end();
    })
  });
}

/**
 * [点击查询重点单位报警器]
 * @param  {Function} callback [description]
 * @return {[type]}            [description]
 */
function getImEnterpriseBySingleClick(callback,geoText){
  var client = new pg.Client(conString2);
  client.connect(function(err) {
    if (err) {
      console.error('连接出错', err);
    }
    client.query("select name, ST_AsGeoJSON(geom) as geom, admregion, kind, level from imenterprise t order by ST_Distance( ST_Transform ( t.geom, 4326 ), ST_GeometryFromText('"+geoText+"',4326))  limit 1", function(err, result) {
      if (err) {
        console.error('查询出错', err);
      }
      callback(result.rows);
      client.end();
    })
  });
}

/**
 * [获取所有重点单位]
 * @param  {Function} callback [description]
 * @return {[type]}            [description]
 */
function getAllImEnterprise(callback,geoText){
  var client = new pg.Client(conString2);
  client.connect(function(err) {
    if (err) {
      console.error('连接出错', err);
    }
    client.query("select name, ST_AsGeoJSON(geom) as geom, admregion, kind, level from imenterprise", function(err, result) {
      if (err) {
        console.error('查询出错', err);
      }
      callback(result.rows);
      client.end();
    })
  });
}

/**
 * [获取所有重点单位]
 * @param  {Function} callback [description]
 * @return {[type]}            [description]
 */
function getAllTrain(callback,geoText){
  var client = new pg.Client(conString2);
  client.connect(function(err) {
    if (err) {
      console.error('连接出错', err);
    }
    client.query("select * from fire_train", function(err, result) {
      if (err) {
        console.error('查询出错', err);
      }
      callback(result.rows);
      client.end();
    })
  });
}

/**
 * [安全责任人]
 * @param  {Function} callback [description]
 * @return {[type]}            [description]
 */
function getSafePerson(callback){
  var client = new pg.Client(conString);
  client.connect(function(err) {
    if (err) {
      console.error('连接出错', err);
    }
    client.query("select * from fire_safepersonliable", function(err, result) {
      if (err) {
        console.error('查询出错', err);
      }
      callback(result.rows);
      client.end();
    })
  });
}

/**
 * [安全责任人]
 * @param  {Function} callback [description]
 * @return {[type]}            [description]
 */
function getAlarmHistory(callback){
  var client = new pg.Client(conString);
  client.connect(function(err) {
    if (err) {
      console.error('连接出错', err);
    }
    client.query("select * from fire_alarmhistory", function(err, result) {
      if (err) {
        console.error('查询出错', err);
      }
      callback(result.rows);
      client.end();
    })
  });
}


/**
 * [巡检任务]
 * @param  {Function} callback [description]
 * @return {[type]}            [description]
 */
function getInspectionMission(callback){
  var client = new pg.Client(conString);
  client.connect(function(err) {
    if (err) {
      console.error('连接出错', err);
    }
    client.query("select * from fire_touradm", function(err, result) {
      if (err) {
        console.error('查询出错', err);
      }
      callback(result.rows);
      client.end();
    })
  });
}